package com.youthfilmic.yuejian.util;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
	private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
	private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
	private static final String regEx_space = "\\s*|\t|\r|\n";// 定义空格回车换行符

	/**
	 * @param htmlStr
	 * @return 删除Html标签
	 */
	private static String delHTMLTag(String htmlStr) {
		Pattern p_script = Pattern.compile(regEx_script,
				Pattern.CASE_INSENSITIVE);
		Matcher m_script = p_script.matcher(htmlStr);
		htmlStr = m_script.replaceAll(""); // 过滤script标签

		Pattern p_style = Pattern
				.compile(regEx_style, Pattern.CASE_INSENSITIVE);
		Matcher m_style = p_style.matcher(htmlStr);
		htmlStr = m_style.replaceAll(""); // 过滤style标签

		Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
		Matcher m_html = p_html.matcher(htmlStr);
		htmlStr = m_html.replaceAll(""); // 过滤html标签

		Pattern p_space = Pattern
				.compile(regEx_space, Pattern.CASE_INSENSITIVE);
		Matcher m_space = p_space.matcher(htmlStr);
		htmlStr = m_space.replaceAll(""); // 过滤空格回车标签
		return htmlStr.trim(); // 返回文本字符串
	}

	/**
	 * @title 先获取去掉Html标签之后的内容，然后截取获得
	 * @author Dxywx
	 * @time 2015.5.13
	 * */
	public static String getSubTextFromHtml(String htmlStr , int sub) {
		if (htmlStr == null) return null;
		htmlStr = delHTMLTag(htmlStr);
		htmlStr = htmlStr.replaceAll("&nbsp;", "");
		int len = htmlStr.length();
		return htmlStr.substring(0, sub < len ? sub : len);
	}
	
	/**
	 * @title 获取去掉Html标签之后的内容
	 * @author Dxywx
	 * @time 2015.5.13
	 * 
	 * */
	public static String getTextFromHtml(String htmlStr) {
		if (htmlStr == null) return null;
		htmlStr = delHTMLTag(htmlStr);
		htmlStr = htmlStr.replaceAll("&nbsp;", "");
		return htmlStr;
	}
	public static void main(String[] args) {
		//String str = "<div style='text-align:center;'> 整治“四风”   清弊除垢<br/><span style='font-size:14px;'> </span><span style='font-size:18px;'>公司召开党的群众路线教育实践活动动员大会</span><br/></div>";
		//System.out.println(getTextFromHtml(str));
		//System.out.println(getDateFromString("2015/04/01 22:09"));
		System.out.println(getTextFromEmoij(" " , "默认用户"));
	}

	/**
	 * @title 正则表达式判断email
	 * @instruction 主要是检测email的格式是否正确
	 * @author Dxywx
	 * @time 2015.5.13
	 * */
	public static boolean isEmail(String email){  
         if (null==email || "".equals(email)) return false;    
         Pattern p =  Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配  
         Matcher m = p.matcher(email);  
         return m.matches();  
        }  
	
	/**
	 * @title 从时间字符串里面格式化提取时间
	 * @instruction 用于从表单中读取时间
	 * @author Dxywx
	 * @time 2015.5.13
	 * */
	public static Date getDateFromString(String dateString){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        try {
            return sdf.parse(dateString);
        } catch (ParseException e) {
        	System.out.println("Error!Format Time String is going wrong!");
            return new Date();
        }
	}
	
	/**
	 * @title 用于将Date时间格式化为指定的时间字符串
	 * @instruction 之前用于表单中的时间
	 * @author Dxywx
	 * @time 2015.5.13
	 * */
	public static String getStringFromDate(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		String temp = null;
		try{
			temp = sdf.format(date);
		}catch(Exception e){
			temp = sdf.format(new Date());
		}
		return temp;
	}

	/**
	 * @title 判断参数是否为null或者空
	 * @param args 变长参数
	 * @author dxywx
	 * @time 2015.9.19
	 * */
	public static boolean isEmpty(String... args){
		for(String t : args){
			if (t == null || "".equals(t)){
				return true;
			}
		}
		return false;
	}

	public static String getTextFromEmoij (String ori , String defaultName){
		if (ori == null) return defaultName;
		String t = filterEmoji(ori);
		t = t.replace(" " , "");
		if (t == null || "".equals(t)) return defaultName;
		return t;
	}


	/**
	 * 检测是否有emoji字符
	 * @param source
	 * @return 一旦含有就抛出
	 */
	private static boolean containsEmoji(String source) {
		if (StringUtils.isBlank(source)) {
			return false;
		}

		int len = source.length();

		for (int i = 0; i < len; i++) {
			char codePoint = source.charAt(i);

			if (isEmojiCharacter(codePoint)) {
				//do nothing，判断到了这里表明，确认有表情字符
				return true;
			}
		}

		return false;
	}

	private static boolean isEmojiCharacter(char codePoint) {
		return (codePoint == 0x0) ||
				(codePoint == 0x9) ||
				(codePoint == 0xA) ||
				(codePoint == 0xD) ||
				((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
				((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
				((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
	}

	private static String filterEmoji(String source) {
		if (!containsEmoji(source)) {
			return source;//如果不包含，直接返回
		}
		//到这里铁定包含
		StringBuilder buf = null;

		int len = source.length();

		for (int i = 0; i < len; i++) {
			char codePoint = source.charAt(i);

			if (isEmojiCharacter(codePoint)) {
				if (buf == null) {
					buf = new StringBuilder(source.length());
				}

				buf.append(codePoint);
			}
		}
		if (buf == null) {
			return source;//如果没有找到 emoji表情，则返回源字符串
		} else {
			if (buf.length() == len) {//这里的意义在于尽可能少的toString，因为会重新生成字符串
				buf = null;
				return source;
			} else {

				return buf.toString();
			}
		}
	}

	public static String getDayString(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		return sdf.format(date);
	}

	
}
